public class Offer58 {
    public String reverseWords(String s) {
        String trim = s.trim();
        StringBuilder stringBuilder = new StringBuilder();
        int j = trim.length()-1,i =j;
        while (i>=0){
            while (i>=0&&trim.charAt(i)!=' ') i--;
            stringBuilder.append(trim.substring(i+1,j+1)+" ");
            while (i>=0&&trim.charAt(i)==' ') i--;
            j=i;
        }
        return stringBuilder.toString().trim();
    }

    public static void main(String[] args) {
        Offer58 offer58 = new Offer58();
        offer58.reverseWords("a good   example");
    }
}
